﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.pdf;
using System.Data;

namespace Supermarkets
{
    class Writer2
    {
        private double total;

        Dictionary<int, string> products;
        Document doc;
        PdfPTable table;
        public Writer2(Dictionary<int, string> products)
        {
            total = 0;
            this.products = products;
            doc = new Document();
            PdfWriter.GetInstance(doc, new FileStream("Sales Report.pdf", FileMode.Create));

            table = new PdfPTable(5);

            PdfPCell product = new PdfPCell(new Phrase("Product"));
            product.BackgroundColor = CMYKColor.LIGHT_GRAY;

            PdfPCell quantity = new PdfPCell(new Phrase("quantity"));
            quantity.BackgroundColor = CMYKColor.LIGHT_GRAY;

            PdfPCell unitPrice = new PdfPCell(new Phrase("unitPrice"));
            unitPrice.BackgroundColor = CMYKColor.LIGHT_GRAY;

            PdfPCell location = new PdfPCell(new Phrase("location"));
            location.BackgroundColor = CMYKColor.LIGHT_GRAY;

            PdfPCell sum = new PdfPCell(new Phrase("Product"));
            sum.BackgroundColor = CMYKColor.LIGHT_GRAY;

            table.AddCell(product);
            table.AddCell(quantity);
            table.AddCell(unitPrice);
            table.AddCell(location);
            table.AddCell(sum);
        }

        public void Write(DataTable dt)
        {
            for (int row = 3; row < dt.Rows.Count - 2; row++)
            {
                string s = (string)dt.Rows[row][0];
                int p = int.Parse(s);
                table.AddCell(products[p] + "");
                table.AddCell((string)dt.Rows[row][1]);
                table.AddCell((string)dt.Rows[row][2]);
                table.AddCell((string)dt.Rows[1][0]);
                table.AddCell((string)dt.Rows[row][3]);
            }

            string s1 = dt.Rows[dt.Rows.Count - 1][3].ToString();
            total += double.Parse(s1);
        }

        public void Close()
        {
            PdfPCell cell = new PdfPCell(new Phrase("Total : "));
            cell.Colspan = 4;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;

            PdfPCell cell1 = new PdfPCell(new Phrase(total + ""));
            cell1.HorizontalAlignment = Element.ALIGN_CENTER;

            table.AddCell(cell);
            table.AddCell(cell1);

            doc.Add(table);
            doc.Close();
        }

        public void Open()
        {
            doc.Open();
        }
        public void AddCell(string str)
        {
            PdfPCell cell = new PdfPCell(new Phrase(str));
            table.AddCell(cell);
        }
    }
}
